package com.store.app.activity;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.R;
import com.store.app.bean.EventBusBean;
import com.store.app.http.Stores;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.SettingData;

import org.json.JSONException;
import org.json.JSONObject;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/4/20.
 * 会员页面
 */
public class MembersActivity extends BaseActivity implements IOnHttpListener {
    private TextView activefamily_lin, regedit_lin;
    private TextView title_tv;
    private TextView title_right;
    private NetRequest netRequest;
    private TextView tv_voucher;
    private TextView tv_reg;
    private TextView tv_card;
    private TextView tv_trade;
    private TextView vip_cash;
    private TextView ll_send_coupon;

    private View btn1, btn2, btn3, btn4;
    private RelativeLayout rl_couple_exchange;
    private Typeface mFace = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_members);
        initView();
        ExitApplication.getInstance().addActivity(this);

        initViews();
        initData();
        initListeners();
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        title_tv.setText("会员");
        netRequest = new NetRequest(this);
        netRequest.requestMembersCount(1);
    }

    private void initListeners() {
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        activefamily_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (SettingData.HYLX_02.equals(Stores.stores_type_key)) {
                        showToast("该功能已被禁用，如有疑问请咨询客服。");
                    } else {
                        Intent intent2ActiveFamilyActivity = new Intent(MembersActivity.this, ActiveFamilyActivity.class);
                        startActivity(intent2ActiveFamilyActivity);
                    }
                } catch (Exception e) {
                    Intent intent2ActiveFamilyActivity = new Intent(MembersActivity.this, ActiveFamilyActivity.class);
                    startActivity(intent2ActiveFamilyActivity);
                    e.printStackTrace();
                }
            }
        });
        regedit_lin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MembersActivity.this, MembersRegeditActivity.class);
                startActivity(intent);
            }
        });
        title_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent2ActiveFamilyActivity = new Intent(MembersActivity.this, MembersDTActivity.class);
                startActivity(intent2ActiveFamilyActivity);
            }
        });

        vip_cash.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MembersActivity.this, CouponGoCashActivity.class);
                startActivity(intent);
            }
        });

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MembersActivity.this, MembersCouponDetailActivity.class);
                startActivity(intent);
            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MembersActivity.this, MembersRegistDetailActivity.class);
                startActivity(intent);
            }
        });
        btn3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MembersActivity.this, MembersOpenCardDetailActivity.class);
                startActivity(intent);
            }
        });
        btn4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //跳到交易界面
                Intent intent = new Intent(MembersActivity.this, MembersTradeRecordActivity.class);
                startActivity(intent);
            }
        });

        rl_couple_exchange.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MembersActivity.this,CoupleConsume.class);
                startActivity(intent);
            }
        });
    }

    /**
     * 激活親情卡之後會發消息過來取刷新
     *
     * @param busBean
     */
    private void onEvent(EventBusBean busBean) {
        if (busBean.type == 200) {
            netRequest.requestMembersCount(1);
        }
    }

    private void initViews() {
        title_tv = (TextView) findViewById(R.id.tvTitle);
        rl_couple_exchange = (RelativeLayout) findViewById(R.id.rl_couple_exchange);
        title_right = (TextView) findViewById(R.id.tv_right1);
        title_right.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.member_dt_icon, 0);
        activefamily_lin = (TextView) findViewById(R.id.activefamily_lin);
        regedit_lin = (TextView) findViewById(R.id.regedit_lin);
        tv_voucher = (TextView) findViewById(R.id.member_tv1);
        tv_reg = (TextView) findViewById(R.id.member_tv2);
        tv_card = (TextView) findViewById(R.id.member_tv3);
        tv_trade = (TextView) findViewById(R.id.member_tv4);
        btn1 = findViewById(R.id.member_tv1_layout);
        btn2 = findViewById(R.id.member_tv2_layout);
        btn3 = findViewById(R.id.member_tv3_layout);
        btn4 = findViewById(R.id.member_tv4_layout);
        vip_cash = (TextView) findViewById(R.id.vip_cash);
        changeFont();

    }

    /**
     * 更改字体
     */
    private void changeFont() {
        TextView[] changeFontTv = {
                tv_voucher, tv_reg, tv_card, tv_trade,
        };
        mFace = Typeface.createFromAsset(getAssets(), "IMPACT.TTF");
        for (TextView tv : changeFontTv) {
            tv.setTypeface(mFace);
        }
    }

    @Override
    public void onAfinalFail() {

    }


    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            try {
                Log.i("mylog", result);
                JSONObject jsonObject = new JSONObject(result);
                String total_voucher = jsonObject.getString("voucher");
                String total_reg = jsonObject.getString("reg");
                String total_card = jsonObject.getString("card");
                String total_trade = jsonObject.getString("trade");
                if (!TextUtils.isEmpty(total_voucher)) {
                    tv_voucher.setText(total_voucher);
                }
                if (!TextUtils.isEmpty(total_reg)) {
                    tv_reg.setText(total_reg);
                }
                if (!TextUtils.isEmpty(total_card)) {
                    tv_card.setText(total_card);
                }
                if (!TextUtils.isEmpty(total_trade)) {
                    tv_trade.setText(total_trade);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void onExecuteFail(int requestId, String msg) {

    }

    private void initView() {
        ll_send_coupon = (TextView) findViewById(R.id.ll_send_coupon);
        ll_send_coupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(Stores.stores_type_key==null){
                    showToast("该功能已被禁用，如有疑问请咨询客服。");
                    return;
                }
                if (SettingData.HYLX_02.equals(Stores.stores_type_key)) {
                    showToast("该功能已被禁用，如有疑问请咨询客服。");
                } else  {
                    Intent intent = new Intent(MembersActivity.this, CouponGoSendActivity.class);
                    startActivity(intent);
                }
            }
        });
    }
}
